﻿using AutoMapper;
using HuaHan.WorkTicket.Domain;
using HuaHan.WorkTicket.Domain.Model;
using HuaHan.WorkTicket.Domain.Repositories;
using HuaHan.WorkTicket.Domain.Repositories.EntityFramework;
using HuaHan.WorkTicket.ServiceContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorkTicket.Application.Implementation
{
    public class SizeGroupServiceImpl : BaseServiceImpl<SizeGroup>,ISizeGroupService
    {

        #region Private Fields
        private UnitOfWork unitOfWork = new UnitOfWork();
        private GenericRepository<SizeGroupSize> sizeGroupSizeRepository;
        private GenericRepository<Size> sizeRepository;
        #endregion

        public SizeGroupServiceImpl()
        {
            base.Repository = unitOfWork.SizeGroupRepository;
            sizeGroupSizeRepository = unitOfWork.SizeGroupSizeRepository;
            sizeRepository = unitOfWork.SizeRepository;
            base.UnitOfWork = this.unitOfWork;
        }

        public override void Add(SizeGroup entity)
        {
            Repository.Add(entity);
            foreach (var item in entity.Sizes)
            {
                SizeGroupSize sizeGroupSize = new SizeGroupSize();
                sizeGroupSize.SizeGroupSizeID = Guid.NewGuid();
                sizeGroupSize.SizeGroupID = entity.SizeGroupID;
                sizeGroupSize.SizeID = item.SizeID;
                sizeGroupSizeRepository.Add(sizeGroupSize);
            }
            
            UnitOfWork.Commit();
        }

        public override SizeGroup GetItemByID(string id)
        {
            SizeGroup entity = Repository.GetByKey(Guid.Parse(id));
            entity.Sizes = new List<Size>();
            foreach (var item in sizeGroupSizeRepository.GetAll().Where(c => c.SizeGroupID == Guid.Parse(id)))
            {
                Size size = new Size();
                size = sizeRepository.GetByKey(item.SizeID);
                entity.Sizes.Add(size);
            }
            return entity;
        }

        public override void Update(SizeGroup entity)
        {
            entity.UpdatedAt = DateTime.Now;
            Repository.Update(entity);

            foreach (var item in sizeGroupSizeRepository.GetAll().Where(c => c.SizeGroupID == entity.SizeGroupID))
            {
                sizeGroupSizeRepository.Delete(item.SizeGroupSizeID);
            }

            foreach (var item in entity.Sizes)
            {
                SizeGroupSize sizeGroupSize = new SizeGroupSize();
                sizeGroupSize.SizeGroupSizeID = Guid.NewGuid();
                sizeGroupSize.SizeGroupID = entity.SizeGroupID;
                sizeGroupSize.SizeID = item.SizeID;
                sizeGroupSizeRepository.Add(sizeGroupSize);
            }
            UnitOfWork.Commit();
        }


        public override void Delete(string id)
        {
            SizeGroup entity = Repository.GetByKey(Guid.Parse(id));
            entity.IsDeleted = true;
            entity.UpdatedAt = DateTime.Now;
            Repository.Update(entity);

            foreach (var item in sizeGroupSizeRepository.GetAll().Where(c=>c.SizeGroupID==entity.SizeGroupID))
            {
                item.IsDeleted = true;
                item.UpdatedAt = DateTime.Now;
                sizeGroupSizeRepository.Update(item);
            }

            UnitOfWork.Commit();
        }


        public bool CheckNameExisted(string name, Guid id)
        {
            var count = base.Repository.GetAll().Where(c => c.Name == name && c.SizeGroupID != id).Count();
            if (count > 0)
                return false;
            else
                return true;
        }

    }
}
